{% extends "../templates/ajax/main_content_base.html" %} 
{% block intro %}{% endblock %}
{% block page_name %}my_group{% endblock %}
{% block content %}


<div class="content-box">
	<div class="content-box-header">
		<h3>{{user.group.name}}</h3>
	</div>
	<!-- End .content-box-header -->

	<div class="content-box-content">

		<div class="tab-content default-tab">

			<h4>成员信息</h4>
			<table>

				<thead>
					<tr>
						<th>学号</th>
						<th>姓名</th>
						<th>邮箱</th>
						<th>角色</th>
						<th>上次更新作业</th>
						<th>操作</th>
					</tr>

				</thead>

				<tfoot>
					<tr>
						<td colspan="5">
							{% ifequal user.group.administrator.student_id  user.student_id %}
							<div class="bulk-actions align-left">
							<form id="delete-group-form" action="delete_group "  method="get">
								<input name="group" type="hidden" value="{{user.group.id}}"/>
								<input type="submit"  class="button"  value="解散该组">
							</form>
							</div>
							{% endifequal %}
							<div class="bulk-actions align-left">
							<form id="quit-group-form" action="quit_group" method="get">
								<input name="group" type="hidden" value="{{user.group.id}}"/>
								<input type="submit" class="button"  value="退出该组"/>
							</form>
							</div>
							
							<div class="clear"></div>
						</td>
					</tr>
				</tfoot>

				<tbody>

				{%for member in user.group.users.all%}
					<tr>
						<td>{{member.student_id}}</td>
						<td>{{member.name}}</td>
						<td>{{member.email}}</td>
						
						<td>
						
						{% ifequal user.group.administrator.student_id  member.student_id %}
							<span style="color:red">组长</span>
						{% else %}
							组员
						{% endifequal %}
						</td>
						<td>10分钟前</td>
						
						<td>
						{% ifequal user.group.administrator.student_id  user.student_id %}
						{% ifnotequal user.group.administrator.student_id  member.student_id %}
							<!-- Icons --> 
							<a class="set-admin" name="{{member.student_id}}" href="javascript:void(0);" title="设为管理员"><img
								src="resources/images/icons/pencil.png" alt="Edit" />
							</a> 
							<a class="kick-out" name="{{member.student_id}}" href="javascript:void(0);" title="踢出"><img
								src="resources/images/icons/cross.png" alt="Delete" />
							</a>
						{% endifnotequal %}
						{% endifequal %}
						</td>
						
					</tr>
				{% endfor %}
					


				</tbody>

			</table>

		</div>
		<!-- End #tab3 -->

	</div>
	<!-- End .content-box-content -->

</div>
<!-- End .content-box -->
{% ifequal user.group.administrator.student_id  user.student_id %}
<div class="content-box">
	<div class="content-box-header">
		<h3>更改小组名称</h3>
	</div>
	<!-- End .content-box-header -->

	<div class="content-box-content">

		<div class="tab-content default-tab">

			<form id="edit-group-name-form" action="edit_group_name" method="get">

				<fieldset>
					<!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->

					<p>
					<input type="hidden" value="{{user.group.id}}" name="group"/>
						<label>新的小组名称</label> <input
						
							class="text-input small-input required" type="text" id="new_name"
							name="new_name" /> 
						<!-- Classes for input-notification: success, error, information, attention -->
						<br />
					</p>
					
					
					<p>
						<input class="button" type="submit" value="更改" />
					</p>

				</fieldset>

				<div class="clear"></div>
				<!-- End .clear -->

			</form>
			

		</div>
		<!-- End #tab3 -->

	</div>
	<!-- End .content-box-content -->

</div>
<!-- End .content-box -->

<script type="text/javascript" >
$.each($('.set-admin'),function(index,obj){
	if($(obj).attr('name')!='')
		$(obj).bind('click',function(event)
				{
			Boxy.ask("您确定要更换管理员吗", ['确定','取消'],function(val) {
				if(val=='确定')
					{
							$.get("change_administrator",
							{"group":"{{user.group.id}}","user":$(obj).attr('name')},
							function(json)
							{
								if(json.error)
				    			{
				    				Boxy.alert("更换管理员失败，请检查您的信息\n"+json.error_type+json.error, function() {}, {title: '错误'});
				    			}
				    			else
				    			{
				    				Boxy.alert("更换管理员成功", function() {$('#main-content').load('my_group')}, {title: '成功'});
				    			}
							},
							"json"
							);
					}
			}, {title: '警告'});
		});

});

$.each($('.kick-out'),function(index,obj){
	if($(obj).attr('name')!='')
		$(obj).bind('click',function(event)
				{
			Boxy.ask("您确定要踢出该用户吗", ['确定','取消'],function(val) {
				if(val=='确定')
					{
							$.get("quit_user",
							{"group":"{{user.group.id}}","user":$(obj).attr('name')},
							function(json)
							{
								if(json.error)
				    			{
				    				Boxy.alert("踢出失败，请检查您的信息"+json.error_type, function() {}, {title: '错误'});
				    			}
				    			else
				    			{
				    				Boxy.alert("踢出成功", function() {$('#main-content').load('my_group')}, {title: '成功'});
				    			}
							},
							"json"
							);
					}
			}, {title: '警告'});
		});

});
$('#delete-group-form').submit(function() { 
	Boxy.ask("您确定解散该小组吗", ['确定','取消'],function(val) {
		if(val=='确定')
			{
		$('#delete-group-form').ajaxSubmit({
			dataType: 'json',
			success:function(json)
    		{
    			if(json.error)
    			{
    				Boxy.alert("解散失败，请检查您的信息"+json.error_type, function() {}, {title: '错误'});
    			}
    			else
    			{
    				Boxy.alert("解散成功", function() {$('#main-content').load('my_group')}, {title: '成功'});
    			}
    		}
    			
		
		});
			}
	}, {title: '警告'});
    return false; 
});
</script>
<script type="text/javascript" >


$('#edit-group-name-form').validate();
$('#edit-group-name-form').ajaxForm({
    // dataType identifies the expected content type of the server response
    dataType: 'json',

    // success identifies the function to invoke when the server response
    // has been received
    success:function(json)
    {
    	if(json.error)
    		{
    		if(json.error_type=="GroupNameDuplication")
				Boxy.alert("组名已经存在，请更换另一个", function() {}, {title: '错误'});
    		else
    			Boxy.alert("修改错误，请检查您的信息"+json.error_type+json.error, function() {}, {title: '错误'});
    		}
    	else
    		Boxy.alert("小组修改成功", function() {
    			$('#main-content').load('my_group')
    		}, {title: '成功'});
    }
});
</script>
{% endifequal %}

<script type="text/javascript" >
$('#quit-group-form').submit(function() { 
	Boxy.ask("您确定退出该小组吗", ['确定','取消'],function(val) {
		if(val=='确定')
			{
		$('#quit-group-form').ajaxSubmit({
			dataType: 'json',
			success:function(json)
    		{
    			if(json.error)
    			{
    				if(json.error_type=="UserIsAdministrator")
    					Boxy.alert("您是该组的组长，当该组还有其他组员时，您不能退出。如果您要退出，请先指定其他组长", function() {}, {title: '错误'});
        			else
    				
    					Boxy.alert("退出失败，请检查您的信息\n"+json.error_type+json.error, function() {}, {title: '错误'});
    			}
    			else
    			{
    				Boxy.alert("退出成功", function() {$('#main-content').load('my_group')}, {title: '成功'});
    			}
    		}
		
		});
			}
	}, {title: '警告'});
    return false; 
});


</script>
{% endblock %}
